home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / comm / comm5 / nwsbrkr5.lha / Reader / NewsBreaker / src / ums / Display.c next >
C/C++ Source or Header  |  1996-12-16  |  9KB  |  350 lines

  1. /*---------------------------------------------------------------*/
  2. /* Routinen zur Ausgabe von Texten, Infos und UMS-Messages       */
  3. /*---------------------------------------------------------------*/
  4. /*
  5.  * DisplayTxt
  6.  * GetTxt
  7.  * FreeTxt(STRPTR txtbuffer)
  8.  * InfoBarClear
  9.  * TextfieldMethods
  10.  * InfoBarUpdate
  11.  * DisplayMsg -- Hauptroutine
  12.  */
  13.  
  14. #define RP win->RPort
  15.  
  16. /*
  17.  * DisplayTxt -- Gibt Message-Text in einem Textfield aus
  18.  *
  19.  * INPUTS
  20.  *   txtfld  - Zeiger auf Textfield-Gadget
  21.  *   win - Zeiger auf aktuelles Fenster
  22.  *   text  - STRPTR auf Messagetext
  23.  *
  24.  * RESULT
  25.  *   TRUE - Ausage OK
  26.  */
  27.  
  28. BOOL
  29. DisplayTxt (Gadget * txtfld, Window * win, STRPTR text)
  30. {
  31.   BOOL success = FALSE;
  32.  
  33.   /* Übergebe Textstring an Textfield-Gadget */
  34.  
  35.   success = (BOOL) (SetGadgetAttrsX (txtfld, win, NULL,
  36.                      TEXTFIELD_Text, text,
  37.                      TAG_END));
  38.   return (success);
  39. }
  40.  
  41.  
  42.  
  43. /*
  44.  * GetTxt -- hole Textstring aus Textfeld. 
  45.  *
  46.  * INPUTS
  47.  *   Gadget * txtfld -- Textfieldgadget
  48.  *   Window * win    -- Window
  49.  *
  50.  * RESULT
  51.  *   STRPTR -- Nullterminierter Text. Mit free() deallozieren!
  52.  *             Oder NULL bei Fehler.
  53.  */
  54.  
  55. STRPTR
  56. GetTxt(Gadget * txtfld, Window * win)
  57. {
  58.   /* Textfield-Handling Puffer und Pointer */
  59.   ULONG txtbuffer = NULL;        /* Textbuffer */
  60.   ULONG txtsize = 0;        /* Länge Textbuffers (byte) */
  61.   STRPTR buffer = NULL;        /* dynamisch allozierter Textpuffer */
  62.  
  63.   if (txtfld && win)
  64.     {
  65.       SetGadgetAttrsX (txtfld, win, NULL,
  66.                TEXTFIELD_ReadOnly, TRUE,    /* sperren */
  67.                TAG_DONE);
  68.  
  69.       GetAttr (TEXTFIELD_Text, txtfld, &txtbuffer);    /* Bufferadresse lesen */
  70.       GetAttr (TEXTFIELD_Size, txtfld, &txtsize);    /* Bufferlänge lesen */
  71.  
  72.       if (txtsize && txtbuffer)    /* ist wirklich Text im Buffer? */
  73.     if (buffer = malloc (txtsize + 1))    /* kann Speicher reserviert werden? */
  74.       {
  75.         /* Text aus Textfield-Puffer kopieren */
  76.         memcpy ((ULONG *) buffer, (ULONG *) txtbuffer, txtsize);
  77.  
  78.             SetGadgetAttrsX (txtfld, win, NULL,
  79.                TEXTFIELD_ReadOnly, FALSE,
  80.                TAG_DONE);
  81.            }
  82.     }
  83.   return ((STRPTR) txtbuffer); /* diesen Pointer zum Deallozieren in free() einsetzen! */
  84. }
  85.  
  86. void
  87. FreeTxt(STRPTR txtbuffer)
  88. {
  89. if (txtbuffer)
  90.  {
  91.  free(txtbuffer);
  92.  }
  93. }
  94.  
  95.  
  96. /*
  97.  * InfoBarClear - löscht die Info-Bar.
  98.  *
  99.  * INPUTS
  100.  *   win- Zeiger auf aktuelles Fenster
  101.  *   height  - Info-Bar Höhe
  102.  *
  103.  * RESULT
  104.  *   void
  105.  */
  106.  
  107. void
  108. InfoBarClear (Window * win, WORD height)    /* todo: region/rectangle */
  109. {
  110.   SetABPenDrMd (RP, pens[FILLPEN], pens[FILLTEXTPEN], JAM1);
  111.   RectFill (RP,
  112.         win->BorderLeft,
  113.         win->BorderTop,
  114.         win->Width - win->BorderRight - win->BorderLeft + 3,
  115.         win->BorderTop + height);
  116.  
  117.   SetABPenDrMd (RP, pens[SHINEPEN], pens[FILLTEXTPEN], JAM1);
  118.   Move (RP, win->BorderLeft, win->BorderTop + height);
  119.   Draw (RP, win->Width - win->BorderRight, win->BorderTop + height);
  120. }
  121.  
  122. /*---------------------------------------------------------*/
  123. /* Variablen für Display-Feld */
  124. /*---------------------------------------------------------*/
  125.  
  126. typedef struct myDispType
  127. {
  128.   ULONG FrontPen;
  129.   ULONG BackPen;
  130.   ULONG DrawMode;
  131.   ULONG LeftEdge;
  132.   ULONG TopEdge;
  133.   STRPTR Head;
  134.   ULONG Tag;
  135.   ULONG Len;
  136. };
  137.  
  138. /* todo: Koords während Laufzeit rechnen */
  139. #define SPALTE1 10
  140. #define SPALTE2 200
  141. #define SPALTE3 350
  142.  
  143. #define ZEILE1  20 /* y-Koordinate */
  144. #define ZEILE2  29
  145. #define ZEILE3  38
  146.  
  147. #define BREITE1 200
  148. #define BREITE2 300
  149.  
  150. /* aus intuition/screens.h */
  151.  
  152. ///erlaubte Pen-Bezeichner:
  153. ///DETAILPEN BLOCKPEN TEXTPEN SHINEPEN SHADOWPEN FILLPEN FILLTEXTPEN
  154. ///BACKGROUNDPEN HIGHLIGHTTEXTPEN BARDETAILPEN BARBLOCKPEN BARTRIMPEN
  155.  
  156. #define myDisplayElements 3L    /* Anzahl der Texte in der Infobar. todo: dynamisch */
  157. #define DISPLAY_DONE {0, 0, 0, 0, 0, NULL, 0, 0}
  158.  
  159. myDispType DisplayStandard[] =
  160. {
  161.   {HIGHLIGHTTEXTPEN, FILLPEN, JAM1, SPALTE1, ZEILE1, "     Von    ", UMSCODE_FromName, BREITE2},
  162.   {HIGHLIGHTTEXTPEN, FILLPEN, JAM1, SPALTE1, ZEILE2, " Betreff    ", UMSCODE_Subject, BREITE2},
  163.   {TEXTPEN, FILLPEN, JAM1, SPALTE1, ZEILE3, "  Gruppe    ", UMSCODE_Group, BREITE2},
  164.   DISPLAY_DONE,
  165. };
  166.  
  167. myDispType DisplayKurz[] =
  168. {
  169.   {HIGHLIGHTTEXTPEN, FILLPEN, JAM1, SPALTE1, ZEILE1, "S  ", UMSCODE_Subject, SPALTE2 - SPALTE1},
  170.   {HIGHLIGHTTEXTPEN, FILLPEN, JAM1, SPALTE2, ZEILE1, "V  ", UMSCODE_FromName, SPALTE3 - SPALTE2},
  171.   {TEXTPEN, FILLPEN, JAM1, SPALTE3, ZEILE1, "G  ", UMSCODE_Group, BREITE2},
  172.   DISPLAY_DONE,
  173. };
  174.  
  175. #ifndef MIN
  176. #define MIN(a,b)    ((a)<(b)?(a):(b))
  177. #endif
  178.  
  179.  
  180. /*
  181.  * InfoBarUpdate - schreibt neuen Inhalt in die Infobar
  182.  *
  183.  * INPUTS
  184.  *  todo!
  185.  * RESULT
  186.  *  TRUE: erfolgreich
  187.  */
  188.  
  189. // todo: User-definierbare, plazierbare Felder
  190.  
  191. BOOL
  192. InfoBarUpdate (UMSUserAccount p_account, UMSMsgNum p_num, Window * p_win, WORD p_height)
  193. {
  194.   /* Vorläufer neue Globalstruktur */
  195.   UMSUserAccount acc = p_account;
  196.   UMSMsgNum num = p_num;
  197.   Window *win = p_win;
  198.   WORD height = p_height;
  199.   myDispType *disp = NULL;
  200.  
  201.   STRPTR myInfo;        /* Stringbuffer */
  202.   int i = 0;            /* Schleifenzähler */
  203.  
  204.   ULONG fit = 0;        /* TextFit(): Anzahl Zeichen */
  205.   struct TextExtent myExtent;    /* Ergebnis von TextFit() */
  206.  
  207.   disp = (myDispType *) & DisplayKurz;    /* fieser CAST, todo, check! */
  208.   InfoBarClear (win, height);    /* InfoBar löschen */
  209.  
  210.   if (num == 0)            /* Messagenummer ist nicht Null? */
  211.     return (FALSE);
  212.  
  213.   myInfo = malloc (200);    /* Buffer alloziert? */
  214.   if (!myInfo)
  215.     return (FALSE);
  216.  
  217.   /* hole die Feldinhalte aus UMS: */
  218.   if (!UMSReadMsgTags (acc,
  219.                UMSTAG_RMsgNum, num,    /* von Message Nr. "num": */
  220.                UMSTAG_RTextFields, &myFields,    /* Headerfields lesen */
  221.                UMSTAG_RReadHeader, NULL,    /* nur die Headerfields lesen */
  222.                UMSTAG_RNoUpdate, 1L,    /* OLD-Flag bleibt wie es ist */
  223.                TAG_DONE))
  224.     {
  225.       CheckErr ();
  226.     }
  227.   else
  228.     {
  229.       while (i < myDisplayElements)
  230.           {
  231.       SetABPenDrMd (RP,
  232.             (ULONG) (pens[disp[i].FrontPen]),    /* Stiftfarben und Zeichenmodus setzen */
  233.             (ULONG) (pens[disp[i].BackPen]),
  234.             (ULONG) (disp[i].DrawMode)
  235.         );
  236.  
  237.       Move (RP,
  238.         (ULONG) (disp[i].LeftEdge),    /* Grafikcursor setzen */
  239.         (ULONG) (disp[i].TopEdge)
  240.         );
  241.  
  242.       Text (RP,        /* jetzt: */
  243.         (STRPTR) (disp[i].Head),    /* Feldname schreiben (z.B. "Gruppe") kludgy, todo */
  244.         (ULONG) (strlen ((STRPTR) (disp[i].Head)))
  245.         );
  246.  
  247.       /* Feldinhalt setzen, aus (kludge: Globaler) "myFields[UMSFELDNUMMER]". */
  248.  
  249.       /* Ist der Feldinhalt zu lang? */
  250.       fit = TextFit (RP,    /* Textlänge prüfen */
  251.              (STRPTR) (myFields[disp[i].Tag]),
  252.              (ULONG) (strlen ((STRPTR) (myFields[disp[i].Tag]))),
  253.              &myExtent,    /* myExtent ist eine notwendige Dummy-Struktur (Ergebnis-Extent) */
  254.              NULL,
  255.              1,
  256.              (ULONG) (MIN (disp[i].Len, (win->Width - win->BorderLeft - win->BorderRight - disp[i].LeftEdge))),    /* Länge des Textes, es fehlt Länge Feldname! todo */
  257.              (ULONG) (RP->TxHeight + 2)    /* Höhe der Schrift */
  258.         );
  259.  
  260.       Text (RP, (STRPTR) (myFields[disp[i].Tag]), fit);    /* Text ausgeben, aber höchstens "fit" Zeichen. */
  261.       i++;            /* todo Schleife ändern */
  262.     }            /* (while) */
  263.  
  264.       UMSFreeMsg (acc, num);    /* Messagebuffer zurückgeben */
  265.     }
  266.  
  267.   if (myInfo)
  268.     {
  269.     free (myInfo);
  270.     }
  271.  
  272.   return (TRUE);        /* erfolgreich geschrieben */
  273. }
  274.  
  275.  
  276. /*****************************************************************
  277.  *                                                               *
  278.  *                           Display Message                     *
  279.  *                             Hauptroutine                      *
  280.  *                                                               *
  281.  *****************************************************************/
  282.  
  283. /*
  284.  * DisplayMsg - holt Message in Textfeld und setzt Infobar
  285.  *
  286.  * (wichtige Haupt-Routine)
  287.  */
  288.  
  289. /* todo: zuviele Parameter */
  290.  
  291. BOOL
  292. DisplayMsg (Gadget * p_txtfld, Window * p_win, WORD p_height, UMSUserAccount p_account, UMSMsgNum p_num)
  293. {
  294.   Gadget *txtfld = p_txtfld;
  295.   Window *win = p_win;
  296.   WORD height = p_height;
  297.   UMSUserAccount acc = p_account;
  298.   UMSMsgNum num = p_num;
  299.  
  300.   STRPTR text = NULL;
  301.   BOOL success = FALSE;
  302.  
  303.   UMSReadMsgTags (acc, UMSTAG_RMsgNum, num,
  304.           UMSTAG_RMsgText, &text,    /* nur den Text lesen */
  305.           UMSTAG_RNoUpdate, 1L,        /* OLD-Flag bleibt wie es ist */
  306.           TAG_END);
  307.  
  308.   if (text)
  309.     {
  310.       DisplayTxt (txtfld, win, text);
  311.       InfoBarUpdate (acc, num, win, height);
  312.  
  313.       /* UMS-Msgbuffer freigeben */
  314.       UMSFreeMsg (acc, num);
  315.       SetGlobMsgNum (num);    /* Globale setzen (auf die jetzt angezeigte Message)  */
  316.       success = TRUE;
  317.     }
  318.   else
  319.     /* text */
  320.     {
  321.       CheckErr ();        /* Fehlermeldung aus UMS-System */
  322.       InfoBarClear (win, height);    /* falsche Angaben löschen */
  323.       success = FALSE;
  324.     }
  325.  
  326.   return (success);
  327. }
  328.  
  329. /*
  330.  * TextfieldMethods -- spezielle Textfield-Attribute setzen
  331.  *
  332.  * INPUTS
  333.  *  method -- TagItem
  334.  *
  335.  */
  336.  
  337. BOOL
  338. TextfieldMethods(ULONG method)
  339. {
  340. BOOL success = FALSE;
  341.  
  342. if (txtfld && Mywindow)
  343.    {
  344.    SetGadgetAttrsX (txtfld, Mywindow, NULL, method, 0, TAG_DONE);
  345.    success = TRUE;
  346.    }
  347.  
  348. return (success);
  349. }
  350.